<template>
  <div class="search">
    <Card>
      <Form
        ref="searchForm"
        @keydown.enter.native="handleSearch"
        :model="searchForm"
        inline
        :label-width="70"
        class="search-form"
      >
        <Form-item label="商品名称" prop="spuName">
          <Input
            type="text"
            v-model="searchForm.spuName"
            placeholder="请输入商品名称"
            clearable
            style="width: 200px"
          />
        </Form-item>

        <Form-item label="商品编号" prop="spuId">
          <Input
            type="text"
            v-model="searchForm.spuId"
            placeholder="请输入商品编号"
            clearable
            style="width: 200px"
          />
        </Form-item>

        <Form-item label="商品分类" prop="cateLogId">
          <CateCascader
            @selected="selectedCateInAdd"
            :key="new Date().getTime()"
            :cateId="searchForm.cateLogId"
            style="width: 200px"
          />
        </Form-item>

        <Form-item label="商品品牌" prop="brandId">
          <Select v-model="searchForm.brandId" filterable style="width: 200px">
            <Option
              v-for="item in brandList"
              :value="item.brandId"
              :key="item.brandId"
              >{{ item.name }}</Option
            >
          </Select>
        </Form-item>

        <Form-item label="店铺编号" prop="storeId">
          <Input
            type="text"
            v-model="searchForm.storeId"
            placeholder="请输入店铺编号"
            clearable
            style="width: 200px"
          />
        </Form-item>

        <Button
          @click="handleSearch"
          class="search-btn"
          type="primary"
          icon="ios-search"
          >搜索</Button
        >
      </Form>
      <Table
        :loading="loading"
        border
        :columns="columns"
        :data="data"
        ref="table"
        class="mt_10"
      >
        <!-- 商品栏目格式化 -->
        <!-- <template slot="goodsSlot" slot-scope="scope">
          <div style="margin-top: 5px; height: 80px; display: flex">
            <div style="">
              <img :src="scope.row.original" style="height: 60px; margin-top: 3px; width: 60px" />
            </div>

            <div style="margin-left: 13px">
              <div class="div-zoom">
                <a>{{ scope.row.goodsName }}</a>
              </div>
            </div>
          </div>
        </template> -->
        <!-- 商品图片展示 -->
        <template #productImg="{ row }">
          <div class="demo-upload-list" style="height: 100px">
            <img
              :src="row.showImg"
              style="height: 100px; margin-top: 3px; width: 100px"
            />
            <!-- <Image  :src="row.showImg" :fit="fit" width="100px" height="100px" :alt="fit" /> -->
            <div class="demo-upload-list-cover">
              <Icon
                type="ios-eye-outline"
                @click="handleViewGoodsPicture(row)"
              ></Icon>
            </div>
          </div>
        </template>
      </Table>
      <Row type="flex" justify="end" class="mt_10">
        <Page
          :current="searchForm.pageNumber"
          :total="total"
          :page-size="searchForm.pageSize"
          @on-change="changePage"
          @on-page-size-change="changePageSize"
          :page-size-opts="[10, 20, 50]"
          size="small"
          show-total
          show-elevator
          show-sizer
        ></Page>
      </Row>
    </Card>
    <Modal title="View Image" v-model="goodsPictureVisible">
      <img
        :src="previewGoodsPicture"
        v-if="goodsPictureVisible"
        style="width: 100%"
      />
    </Modal>
  </div>
</template>

<script>
import { authGoods, getAuthGoodsListData } from '@/api/goods';
import CateCascader from '@/views/lili-components/CateCascader.vue';
import * as APIBrand from '@/api/myapi/Brand.js';
import * as APISpu from '@/api/myapi/Spu.js';
export default {
  name: 'goods',
  components: { CateCascader },
  data() {
    return {
      // 图片是否展示
      goodsPictureVisible: false,
      previewGoodsPicture: '', // 展示图片的历史
      id: '', //要操作的id
      loading: true, // 表单加载状态
      searchForm: {
        // 搜索框初始化对象
        pageNumber: 1, // 当前页数
        pageSize: 10, // 页面大小
        sort: 'create_time', // 默认排序字段
        order: 'desc', // 默认排序方式
      },
      goodsAuditForm: {
        // 商品编辑表单
        auth_flag: 1,
      },
      columns: [
        {
          title: '商品编号',
          key: 'id',
          minWidth: 200,
          tooltip: true,
        },
        {
          title: '商品名称',
          key: 'spuName',
          minWidth: 140,
          tooltip: true,
        },

        {
          title: '商品图片',
          key: 'showImg',
          slot: 'productImg',
          minWidth: 110,
        },
        {
          title: '商品分类',
          key: 'cateName',
          minWidth: 120,
          align: 'center',
          tooltip: true,
        },
        {
          title: '商品品牌',
          key: 'brandName',
          minWidth: 120,
          align: 'center',
          tooltip: true,
        },
        // {
        //   title: "审核状态",
        //   key: "authFlag",
        //   minWidth: 130,
        //   render: (h, params) => {
        //     if (params.row.authFlag == "TOBEAUDITED") {
        //       return h("div", [
        //         h("Badge", {
        //           props: {
        //             status: "error",
        //             text: "待审核",
        //           },
        //         }),
        //       ]);
        //     } else if (params.row.authFlag == "PASS") {
        //       return h("div", [
        //         h("Badge", {
        //           props: {
        //             status: "success",
        //             text: "审核通过",
        //           },
        //         }),
        //       ]);
        //     } else if (params.row.authFlag == "REFUSE") {
        //       return h("div", [
        //         h("Badge", {
        //           props: {
        //             status: "error",
        //             text: "审核拒绝",
        //           },
        //         }),
        //       ]);
        //     }
        //   },
        // },

        {
          title: '店铺名称',
          key: 'storeName',
          align: 'center',
          minWidth: 100,
          tooltip: true,
        },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          fixed: 'right',
          width: 200,
          render: (h, params) => {
            return h('div', [
              h(
                'Button',
                {
                  props: {
                    type: 'success',
                    size: 'small',
                  },
                  style: {
                    marginRight: '5px',
                  },
                  on: {
                    click: () => {
                      this.examine(params.row, 1);
                    },
                  },
                },
                '通过'
              ),
              h(
                'Button',
                {
                  props: {
                    type: 'error',
                    size: 'small',
                  },
                  style: {
                    marginRight: '5px',
                  },
                  on: {
                    click: () => {
                      this.examine(params.row, 0);
                    },
                  },
                },
                '拒绝'
              ),
              h(
                'Button',
                {
                  props: {
                    size: 'small',
                  },
                  on: {
                    click: () => {
                      this.showDetail(params.row);
                    },
                  },
                },
                '查看'
              ),
            ]);
          },
        },
      ],
      data: [], // 表单数据
      total: 0, // 表单数据总数
      brandList: [], // 品牌列表
    };
  },
  methods: {
    init() {
      // 初始化数据
      this.getDataList();
      this.getBrandList();
    },
    changePage(v) {
      // 改变页码
      this.searchForm.pageNumber = v;
      this.getDataList();
    },
    changePageSize(v) {
      // 改变每页数量
      this.searchForm.pageSize = v;
      this.getDataList();
    },
    handleSearch() {
      // 搜索
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      this.getDataList();
    },
    getDataList() {
      // 获取列表数据
      this.loading = true;
      // 带多条件搜索参数获取表单数据
      // this.searchForm.authFlag = 0;
      console.log('测试搜索请求参数:', this.searchForm);
      APISpu.getProductReviewByPage(this.searchForm).then((res) => {
        this.loading = false;
        if (res.data.code == 201) {
          console.log('请求成功', res);
          this.data = res.data.obj.records;
          this.total = parseInt(res.data.obj.total);
        }
      });
      // getAuthGoodsListData(this.searchForm).then((res) => {
      //   this.loading = false;
      //   if (res.records) {
      //     this.data = res.records;
      //     this.total = res.total;
      //   }
      // });
    },
    examine(v, authFlag) {
      // 审核商品
      // let examine = "通过";
      // this.goodsAuditForm.authFlag = "PASS";
      // if (authFlag != 1) {
      //   examine = "拒绝";
      //   this.goodsAuditForm.authFlag = "REFUSE";
      // }
      let examine = '';
      let params = { spuId: null, authStatus: null };
      if (authFlag == 0) {
        examine = '拒绝';
        params.spuId = v.id;
        params.authStatus = 0;
      } else {
        examine = '通过';
        params.spuId = v.id;
        params.authStatus = 1;
      }
      this.$Modal.confirm({
        title: '确认审核',
        content: '您确认要审核' + examine + ' ' + v.spuName + ' ?',
        loading: true,
        onOk: () => {
          APISpu.authSpu(params).then((res) => {
            this.$Modal.remove();
            if (res.data.code == 201) {
              this.$Message.success('操作成功');
              this.getDataList();
            }
          });
        },
      });
    },
    //查看商品详情
    showDetail(v) {
      let id = v.id;
      this.$router.push({
        name: 'goods-detail',
        query: { id: id },
      });
    },
    // 分类选择
    selectedCateInAdd(item) {
      let arr = item[0];
      this.searchForm.cateLogId = arr[2];
    },
    // 获取品牌
    getBrandList() {
      APIBrand.getAllBrand().then((res) => {
        if (res.data.code == 201) {
          this.brandList = res.data.obj;
        }
      });
    },
    // 查看当前图片
    handleViewGoodsPicture(item) {
      this.goodsPictureVisible = true;
      this.previewGoodsPicture = item.showImg;
    },
  },
  mounted() {
    this.init();
  },
};
</script>
<style scoped>
.demo-upload-list {
  display: inline-block;
  width: 160px;
  height: 150px;
  text-align: center;
  line-height: 60px;
  border: 1px solid transparent;
  border-radius: 4px;
  overflow: hidden;
  background: #fff;
  position: relative;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
  margin-right: 4px;
}

.demo-upload-list img {
  width: 100%;
  height: 100%;
}

.demo-upload-list-cover {
  display: none;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.6);
}

.demo-upload-list:hover .demo-upload-list-cover {
  display: block;
}

.demo-upload-list-cover i {
  color: #fff;
  font-size: 40px;
  cursor: pointer;
  margin: 20 20px;
}
</style>
